/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import Model.Model_Category_Question;
import Util.DataUtil;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Admin
 */
public class DAO_Category_Question {

    public ArrayList<Model_Category_Question> getAllCategory() {
        ArrayList<Model_Category_Question> listCategory = new ArrayList<Model_Category_Question>();

        try {
            CallableStatement csmt = DataUtil.getconnect().prepareCall("{call sp_GetAllCategory_Question}");
            ResultSet rs = csmt.executeQuery();
            while (rs.next()) {
                Model_Category_Question category = new Model_Category_Question();
                category.setId(rs.getString("ID"));
                category.setName(rs.getString("Name"));
                category.setLast_update(rs.getString("Last_Update"));

                listCategory.add(category);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return listCategory;
    }

    public Model_Category_Question getOneCategoryByID(String id) {
        Model_Category_Question modCategory = new Model_Category_Question();
        try {
            CallableStatement csmt = DataUtil.getconnect().prepareCall("{Call sp_GetOneCategoryByID(?)}");
            csmt.setString("ID", id);
            ResultSet rs = csmt.executeQuery();
            while (rs.next()) {
                modCategory.setId(rs.getString("ID"));
                modCategory.setName(rs.getString("Name"));
                modCategory.setLast_update(rs.getString("Last_Update"));
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Category_Question.class.getName()).log(Level.SEVERE, null, ex);
        }
        return modCategory;
    }

    //get All category Where name
    public ArrayList<Model_Category_Question> getIDCategoryByName(String name) {
        ArrayList<Model_Category_Question> listCategory = new ArrayList<Model_Category_Question>();
        try {
            CallableStatement csmt = DataUtil.getconnect().prepareCall("{Call sp_GetAllCategory_Question_ByName(?)}");
            csmt.setString("Name", name);
            ResultSet rs = csmt.executeQuery();
            while (rs.next()) {
                Model_Category_Question modeCategory = new Model_Category_Question();
                modeCategory.setId(rs.getString("ID"));
                modeCategory.setName(rs.getString("Name"));
                modeCategory.setLast_update(rs.getString("Last_Update"));
                listCategory.add(modeCategory);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Category_Question.class.getName()).log(Level.SEVERE, null, ex);
        }
        return listCategory;
    }

    public boolean Iinsert_Category(Model_Category_Question category) {
        try {
            CallableStatement csmt = DataUtil.getconnect().prepareCall("{Call sp_insertCategory(?)}");
            csmt.setString("Name", category.getName());
            csmt.execute();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Category_Question.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    
    public boolean Update_Category(Model_Category_Question category) {
        try {
            CallableStatement csmt = DataUtil.getconnect().prepareCall("{Call sp_UpdateCategory(?,?)}");
            csmt.setString("ID", category.getId());
            csmt.setString("Name", category.getName());
            csmt.execute();
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Category_Question.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }
    }
    
     public Integer Delete_Category(String id) {
         Integer result =0;
        try {
            CallableStatement csmt = DataUtil.getconnect().prepareCall("{Call sp_deleteCategory(?,?)}");
            csmt.setString("ID", id);
            csmt.registerOutParameter("RESULT", java.sql.Types.INTEGER);
            csmt.execute();
            result = csmt.getInt("RESULT");
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Category_Question.class.getName()).log(Level.SEVERE, null, ex);
            
        }
        return result;
    }
      
}
